package com.collection.queue;

import com.domain.User;

import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;

public class PriorityQueueDemo {
    public static void main(String[] args) {

        //优先队列自然排序示例
        Queue<Integer> integerPriorityQueue = new PriorityQueue<>(7);
        Random rand = new Random();
        for(int i=0;i<7;i++){
            integerPriorityQueue.add(new Integer(rand.nextInt(100)));
        }
        for(int i=0;i<7;i++){
            Integer in = integerPriorityQueue.poll();
            System.out.println("Processing Integer:"+in);
        }


        //优先队列使用示例
        Queue<User> customerPriorityQueue = new PriorityQueue<>(7, idComparator);
        addDataToQueue(customerPriorityQueue);
        pollDataFromQueue(customerPriorityQueue);
    }


    //匿名Comparator实现

    public static Comparator<User> idComparator = new Comparator<User>(){
        @Override
        public int compare(User c1, User c2) {
        return (int) (c1.getId() - c2.getId());
        }
    };


    //用于往队列增加数据的通用方法
    private static void addDataToQueue(Queue<User> customerPriorityQueue) {
        Random rand = new Random();
        for(int i=0; i<7; i++){
            int id = rand.nextInt(100);
            customerPriorityQueue.add(new User(id, "周一", "123456", 20));
        }
    }



    //用于从队列取数据的通用方法
    private static void pollDataFromQueue(Queue<User> customerPriorityQueue) {
        while(true){
            User cust = customerPriorityQueue.poll();
            if(cust == null) break;
            System.out.println("Processing Customer with ID="+cust.getId());
        }
    }

}
